package com.mindainfo.configCenter.config.web;

import com.mindainfo.configCenter.base.common.DataGrid;
import com.mindainfo.configCenter.base.common.Response;
import com.mindainfo.configCenter.base.common.ResponseFactory;
import com.mindainfo.configCenter.config.bean.RemoteConfig;
import com.mindainfo.configCenter.config.bean.User;
import com.mindainfo.configCenter.config.service.IConfigService;
import com.mindainfo.configCenter.config.vo.RemoteConfigEditVo;
import com.mindainfo.configCenter.config.vo.RemoteConfigPageVo;
import com.mindainfo.configCenter.config.vo.RemoteConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by MD-01 on 2016/11/10.
 */
@Controller
@RequestMapping("/config")
public class ConfigController {

    private Logger log = LoggerFactory.getLogger("配置");

    @Resource
    private IConfigService configService;

    //跳转到list页面
    @RequestMapping("/list")
    public String list(){
        return "config/list";
    }


    //查询配置列表信息
    @RequestMapping("/queryList")
    @ResponseBody
    public DataGrid<RemoteConfigPageVo> queryList(String keyword, String page, String rows){

        return configService.queryList(keyword,page,rows);
    }

    @RequestMapping("/edit")
    public String edit(String id,HttpServletRequest request){

        Response<RemoteConfigVo> vo = configService.getConfig(id);
        if(vo.getCode()==0){
            request.setAttribute("config",vo.getData());
            request.setAttribute("title","修改配置信息");
        }else{
            request.setAttribute("title","新增配置信息");
        }

        return "config/edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Response<Void> save(HttpServletRequest request){

        //配置id
        String id = request.getParameter("id");

        //服务器ip
        String serverHost = request.getParameter("serverHost");
        //服务器端口
        String serverPort = request.getParameter("serverPort");
        //服务器名称
        String serverName = request.getParameter("serverName");
        //数据库ip
        String dbHost = request.getParameter("dbHost");
        //数据库端口
        String dbPort = request.getParameter("dbPort");
        //数据库名称
        String dbName = request.getParameter("dbName");
        //数据库用户名
        String dbUser = request.getParameter("dbUser");
        //数据库密码
        String dbPassword = request.getParameter("dbPassword");

        //websocket服务器ip
        String audioServerHost = request.getParameter("audioServerHost");
        //websocket服务器端口
        String audioServerPort = request.getParameter("audioServerPort");

        //jpush appkey
        String appkey = request.getParameter("appkey");
        //jpush secret
        String masterSecret = request.getParameter("masterSecret");

        //redis host
        String redisHost = request.getParameter("redisHost");
        //redis port
        String redisPort = request.getParameter("redisPort");
        //redis password
        String redisPassword = request.getParameter("redisPassword");

        RemoteConfig config = new RemoteConfig();
        config.setServerHost(serverHost);
        config.setServerPort(Integer.parseInt(serverPort));
        config.setServerName(serverName);
        config.setDbHost(dbHost);
        config.setDbPort(Integer.parseInt(dbPort));
        config.setDbName(dbName);
        config.setDbUser(dbUser);
        config.setDbPassword(dbPassword);
        config.setAudioServerHost(audioServerHost);
        config.setAudioServerPort(Integer.parseInt(audioServerPort));
        config.setAppkey(appkey);
        config.setMasterSecret(masterSecret);
        config.setRedisHost(redisHost);
        config.setRedisPort(Integer.parseInt(redisPort));
        config.setRedisPassword(redisPassword);
        config.setCreateTime(new Date());

        //id不为空,修改
        if(StringUtils.isNotEmpty(id)){
            ObjectId objectId = new ObjectId(id);
            config.setId(objectId);
        }

        return configService.save(config);
    }

    @RequestMapping("/getConfig")
    @ResponseBody
    public Response<RemoteConfigVo> getConfig(String serverHost){

        return configService.getConfig(serverHost);
    }
}
